/* ***********************
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is OisterBake.js module.
 *
 * The Initial Developer of the Original Code is Rob Gerns.
 *
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 **************************/


/****************************************************
* Function Name(s): oisterBake
*
* Purpose:
* Use AJAX to grab the XML file, call the "createSkelton"
* function to set up a basic structure on the HTML page 
* before going on to do anything else.  Call the 
* handleResponse() function only after the XML file is completely
* received and the skeleton structure is ready.
*
*****************************************************/	

function oisterBake(oisterURI) {

 //Just to avoid confusion, if a "container" node is left over from something, remove it
 if(document.getElementById("container") != null && document.getElementById("container") !=undefined){
  clearContainer();
 }
	
 //the URI of the Oister file that is to be read   	
 theoisterfile = document.getElementById(oisterURI).value;
 
 if(theoisterfile=="" || theoisterfile=="Please enter a URL in this space first."){
   document.getElementById(oisterURI).value="Please enter a URL in this space first.";
 }
 else if(theoisterfile!=""){  
 
  if(theoisterfile.indexOf('http://')==-1){
	 theoisterfile = "http://"+theoisterfile;
  }
	
  //start an AJAX GET request for the file	
  http.open("GET", theoisterfile, true);
    
  //Call the createSkelton function to generate some 'div' layers that will be used to contain content later
  createSkeleton();  
	
  //When the XML data is ready, call the handleResponse function (which in turn calls the XML/DOM functions)
  http.onreadystatechange = function(){
   if(http.readyState==4 && http.status==200){ 
    handleResponse();
   }
   else if(http.readyState==4 && http.status==404){
    document.getElementById("begin").style.display="none";
    var errmsg = document.createElement("h1");
    var errmsgId = errmsg.id = "errormsg";
    document.getElementById("container").appendChild(errmsg);
    errmsg.innerHTML = "An Oister feed wasn't found at that URL.  The URL may have changed.";
   }
  }
 
 //Since a GET request is being used to grab the XML, there's nothing to send back to the server
 http.send(null);	
 return true; 
 } 
}  